package cn.jobstart.controller;

import cn.jobstart.common.FileUtil;
import cn.jobstart.dto.ResumeBasicDTO;
import cn.jobstart.dto.ResumeBasicPageDTO;
import cn.jobstart.es.listener.event.ResumeBaseEsUpdateEvent;
import cn.jobstart.es.listener.event.ResumeEsDeleteEvent;
import cn.jobstart.es.service.IResumeEsService;
import cn.jobstart.export.ResumeBasicDomainExport;
import cn.jobstart.modelExt.ResumeBasicExt;
import cn.jobstart.props.FileConfig;
import cn.jobstart.pubInter.IBaseService;
import cn.jobstart.pubInter.exception.SkException;
import cn.jobstart.searchExt.ResumeBasicSearchExt;
import cn.jobstart.service.IResumeBasicService;
import cn.jobstart.vo.*;
import cn.jobstart.web.common.SkJsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @author sunke
 * @Date 2023-09-06 15:00:21
 * @description ResumeBasicController      简历基础表
 */
@Api(tags = "人才管理系统-简历基础表相关", description = "简历基础表相关接口")
@RestController(value = "resumeBasicRestfulController")
@RequestMapping("/resumeBasic/")
@Validated
public class ResumeBasicController extends ApiManageController<ResumeBasicDTO, ResumeBasicPageDTO, ResumeBasicExt, ResumeBasicSearchExt, ResumeBasicDomainVO, ResumeBasicDetailDomainVO, ResumeBasicDetailVO, ResumeBasicListVO, String> {

    @Resource(name = "resumeBasicService")
    private IResumeBasicService resumeBasicService;


    @Resource
    private FileConfig fileConfig;
    @Resource
    private IResumeEsService resumeEsService;
    @Autowired
    private ApplicationEventPublisher applicationEventPublisher;

    @ApiOperation(value = "得到原始简历文件", notes = "得到原始简历文件")
    @PostMapping("getOldResumeFile/{resumeBasicId}")
    public void getOldResumeFile(@PathVariable String resumeBasicId, HttpServletResponse response) throws Exception {

        ResumeBasicExt resumeBasic = resumeBasicService.getDetail(resumeBasicId);
        String oldFile = resumeBasic.getOldResumeFile();


        FileUtil.writeFileToResponse(fileConfig.getRoot() + File.separator + oldFile, resumeBasic.getTruename() + "." + FileUtil.getSuffix(oldFile), response);


    }

    @ApiOperation(value = "人才简历更新列表", notes = "人才简历更新列表")
    @PostMapping(value = "updateResumeList/{personId}")
    public SkJsonResult<List<ResumeBasicUpdateDomainVO>> updateResumeList(@ApiParam(value = "人才ID") @PathVariable String personId) throws SkException {
        return SkJsonResult.ok().setData(mapList(resumeBasicService.getListByPersonId(personId), ResumeBasicUpdateDomainVO.class));
    }

    @Override
    public void afterPostAdd(ResumeBasicDTO dto, ResumeBasicExt domain, ResumeBasicDetailVO resumeBasicDetailVO, HttpServletRequest request, HttpSession session) throws SkException {
        super.afterPostAdd(dto, domain, resumeBasicDetailVO, request, session);

        resumeEsService.publishByResumeId(domain.getId());

    }


    @Override
    public void afterPostMod(ResumeBasicDTO dto, ResumeBasicExt domain, SkJsonResult<ResumeBasicDetailVO> jsonResult, HttpServletRequest request, HttpSession session) throws SkException {
        super.afterPostMod(dto, domain, jsonResult, request, session);
        ResumeBaseEsUpdateEvent event = new ResumeBaseEsUpdateEvent();
        event.setResumeId(domain.getId());
        applicationEventPublisher.publishEvent(event);

    }


    @Override
    public void afterPostDelete(String id, HttpServletRequest request, HttpSession session) throws SkException {
        super.afterPostDelete(id, request, session);
        ResumeEsDeleteEvent event = new ResumeEsDeleteEvent();
        event.setResumeId(id);
        applicationEventPublisher.publishEvent(event);
    }

    @Override
    public IBaseService<ResumeBasicExt, ResumeBasicSearchExt, String> getBaseService() throws SkException {
        return resumeBasicService;
    }

    @Override
    public String getBaseRoute() {
        return "/resumeBasic/";
    }

    /**
     * 当前模板名称
     *
     * @return
     */
    @Override
    public String getModelName() {
        return "简历基础表";
    }

    @Override
    public Class exportExcelClass() {
        return ResumeBasicDomainExport.class;
    }

    @Override
    public Set<String> totalMapSet() {
        Set totalMapSet = new HashSet();
        totalMapSet.add("count(resume_basic.rb_id) as countNum");
        return totalMapSet;
    }


}